package com.zhongli.devplatform.controller.student;

import com.tencentcloudapi.ame.v20190916.models.Item;
import com.zhongli.devplatform.bean.Res;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.dto.student.BindParentDTO;
import com.zhongli.devplatform.dto.student.StudyReportDetailsDTO;
import com.zhongli.devplatform.dto.student.UpdateBindParentDTO;
import com.zhongli.devplatform.po.sys.ReadSubjectPO;
import com.zhongli.devplatform.query.student.StudentQuery;
import com.zhongli.devplatform.service.imp.sys.ReadSubjectService;
import com.zhongli.devplatform.service.imp.user.ReadUserInfoService;
import com.zhongli.devplatform.service.imp.user.ReadUserService;
import com.zhongli.devplatform.service.imp.view.ViewUserService;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.component.FollowUtil;
import com.zhongli.devplatform.vo.sys.SubjectVO;
import com.zhongli.devplatform.vo.student.UserAndClassInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author WuXiCheng
 * @version 1.0
 * @date 2020/7/31 9:15
 */
@Api(tags = "学生信息", description = "学生信息", value = "学生信息")
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private FollowUtil followUtil;

    @Autowired
    private ReadUserService userService;

    @Autowired
    private ViewUserService viewUserService;

    @Autowired
    private ReadSubjectService subjectService;

    @Autowired
    private ReadUserInfoService userInfoService;

//    @ApiOperation("根据老师布置的作业id查询学生列表")
//    @GetMapping("/studentListByOpeId")
//    public Res studentListByOpeId(StudentQuery studentQuery){
//        return userService.studentListByOpeId(studentQuery);
//    }

    /**
     * 获取当前用户的信息
     * @return 结果
     */
    @ApiOperation("获取当前用户的信息")
    @GetMapping("/getCurrentStudentInfo")
    public Res getCurrentStudentInfo(){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        return userInfoService.getCurrentStudentInfo(currentUserId);
//        ReadUserInfoPO data = userInfoService.getOne(new QueryWrapper<ReadUserInfoPO>().eq("user_id", currentUserId));
//        CurrentStudentInfoVO currentStudentInfoVO = BeanCopierUtil.copy(data, CurrentStudentInfoVO.class);
//        ReadUserPO userPO = userService.getById(currentUserId);
//        assert currentStudentInfoVO != null;
//        currentStudentInfoVO.setSoleId(userPO.getSoleId());
//        return Res.ok(currentStudentInfoVO);
    }

    @ApiOperation("学生情况学生列表")
    @GetMapping("/learnCondition")
    public Res learnCondition(StudentQuery query){

        return Res.ok(userService.learnCondition(query));
    }

    @ApiOperation("老师所教班级学生列表")
    @GetMapping("/studentListByClazzId")
    public Res studentListByClazzId(@ModelAttribute StudentQuery studentQuery){
        return userService.studentListByClazzId(studentQuery);
    }

    @ApiOperation("学习报告")
    @GetMapping("/learnReport")
    public Res learnReport(){
        List<ReadSubjectPO> list = subjectService.list();
        List<SubjectVO> subjectVOS = BeanCopierUtil.copy(list, SubjectVO.class);
        return Res.ok(subjectVOS);
    }

    @ApiOperation("查看学生已绑定的家长信息")
    @GetMapping("/getBindParentInfo")
    public Res getBindParentInfo(){
        return userService.getBindParentInfo();
    }

    @ApiOperation("学生绑定家长")
    @PostMapping("/bindParent")
    public Res bindParent(@RequestBody BindParentDTO bindParentDTO){
        return userService.bindParent(bindParentDTO);
    }

    @ApiOperation("修改绑定家长信息")
    @PostMapping("/updateBindParent")
    public Res updateBindParent(@RequestBody UpdateBindParentDTO updateBindParentDTO ){
        return userService.updateBindParent(updateBindParentDTO);
    }

    /**
     * 获取当前老师所教授的所有学生
     * @param studyReportDetailsDTO {@link StudyReportDetailsDTO}
     * @return 学生列表
     */
    @ApiOperation("获取当前老师所教授的所有学生  currentThreadId不需要传递")
    @PostMapping("/getAllStudentByCurrentTeach")
    public Res getAllStudentByCurrentTeach(@RequestBody StudyReportDetailsDTO studyReportDetailsDTO){
        List<UserAndClassInfoVO> data = userService.getAllStudentByCurrentTeach(studyReportDetailsDTO);
        return Res.ok(data);
    }

    @ApiOperation("关注取消关注")
    @GetMapping("/follow")
    public Res follow(@RequestParam("followingId") String followingId){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        String userId = String.valueOf(currentUserId);
        followUtil.addOrRelease(userId,followingId);
        return Res.ok("success");
    }

    @ApiOperation("获取当前用户所有粉丝列表")
    @GetMapping("/getFans")
    public Res getFans(){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        String userId = String.valueOf(currentUserId);
        Set<String> fans = followUtil.findFans(userId);
        return Res.ok(fans);
    }

    @ApiOperation("获取当前用户所有关注的人列表")
    @GetMapping("/getFollwings")
    public Res getFollwings(){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        String userId = String.valueOf(currentUserId);
        Set<String> follwings = followUtil.findFollwings(userId);
        return Res.ok(follwings);
    }

}
